﻿using Autofac;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace Summer.WebApi
{
    public class AutofacModule:Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            //构造函数注入 
            Assembly service = Assembly.Load("Summer.Service");
            builder.RegisterAssemblyTypes(service)
                .AsImplementedInterfaces()
                .InstancePerLifetimeScope()
                //.EnableInterfaceInterceptors()  //开启接口拦截
                //.InterceptedBy(typeof(LogInterceptor))    //设置全局拦截器，统一由LogInterceptor拦截所有接口的调用
                ;

            //属性注入
            var controllerBaseType = typeof(ControllerBase);
            builder.RegisterAssemblyTypes(typeof(Startup).Assembly).
                Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
                .PropertiesAutowired()
                .InstancePerLifetimeScope();

        }
    }
}
